package com.kongxx.struts2study.host.service.mock;

import java.util.ArrayList;
import java.util.List;

import com.kongxx.struts2study.host.model.Host;
import com.kongxx.struts2study.host.service.HostService;

public class HostServiceMockImpl implements HostService {

	private static List<Host> hosts;
	
	static {
		hosts = new ArrayList<Host>();
		for (int i = 1; i <= 10; i++) {
			hosts.add(new Host("" + i, "host" + i, "linux", "192.168.0." + i));
		}
	}
	
	public List<Host> list() {
		return hosts;
	}

	public Host find(String id) {
		for (Host host : hosts) {
			if (id.equals(host.getId())) {
				return host;
			}
		}
		return null;
	}

	public void add(Host host) {
		hosts.add(host);
	}

	public void update(Host host) {
		for (Host h : hosts) {
			if (host.getId().equals(h.getId())) {
				h.setName(host.getName());
				h.setOs(host.getOs());
				h.setIp(host.getIp());
				return;
			}
		}
	}

	public void delete(Host host) {
		for (Host h : hosts) {
			if (host.getId().equals(h.getId())) {
				hosts.remove(host);
				return;
			}
		}
	}
}
